import pandas as pd
import time

def level_replace(x):
    level_str = x
    level_str = level.get(level_str, x)
    if level_str == x:
        for i in level:
            level_str = level_str.replace(i, level[i])
    return level_str


df = pd.read_csv('../static/data/typhoon_info.csv')
df = df.rename(columns={'a': 'id', 'd': 'datetime', 'e': 'level', 'f': 'lng', 'g': 'lat', 'h': 'hPa(中心气压)',
                        'i': 'max_wind_speed', 'j': 'move_direction', 'k': 'move_speed'})
level = {'TD': '热带低压', 'TS': '热带风暴', 'STS': '强热带风暴', 'TY': '台风', 'STY': '强台风', 'SuperTY': '超强台风'}
df = df.loc[:, ['id', 'datetime', 'level', 'lng', 'lat', 'hPa(中心气压)', 'max_wind_speed', 'move_direction', 'move_speed']]
df['datetime'] = df['datetime'].apply(lambda x: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(x / 1000)))
df['level'] = df['level'].apply(lambda x: level_replace(x))
df['move_direction'] = df['move_direction'].apply(
    lambda x: x.replace('N', '北').replace('S', '南').replace('E', '东').replace('W', ''))
df['move_direction'] = df['move_direction'].apply(lambda x: '无' if (x == 'no' or x == '0' or x == '') else x)
df['month'] = df['datetime'].apply(lambda x: x.split('-')[1])
df.to_csv('../static/data/typhoon_info_clean.csv', index=False)


df = pd.read_csv('../static/data/typhoon_list.csv')
df.dropna(axis=0, subset=["cName"], inplace=True)
df['text'] = df['text'].apply(lambda x: '' if pd.isnull(x) else x)
df['text'] = df['text'].apply(lambda x: x.replace('\n','').replace('\r',''))
df.to_csv('../static/data/typhoon_list_clean.csv',index=False)



